﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataLibrary
{
    public class Ctbl_Product:IData<tbl_Product>
    {
        public Ctbl_Product(String myconnecString) : base(myconnecString) { }

        public override List<tbl_Product> GetAll(tbl_Product item)
        {
            throw new NotImplementedException();
        }

        public override tbl_Product GetItemById(object id)
        {
            return GetAll().SingleOrDefault(p => p.IdProduct == Convert.ToInt32(id));
        }

        public override tbl_Product GetItem(IDataReader dr)
        {
            tbl_Product obj = new tbl_Product();
            obj.Description = Convert.ToString(dr[tbl_Product.c_Description]);
            obj.IdProduct = Convert.ToInt32(dr[tbl_Product.c_idProduct]);
            obj.ImageProduct = Convert.ToString(dr[tbl_Product.c_imageProduct]);
            obj.IsActive = Convert.ToInt32(dr[tbl_Product.c_isActive]);
            obj.OrderId = Convert.ToInt32(dr[tbl_Product.c_orderId]);
            obj.Path = Convert.ToString(dr[tbl_Product.c_Path]);
            return obj;
        }

        public override List<tbl_Product> GetAll()
        {
            String strSQL = "select * from " + tbl_Product.b_tbl_Product + strAsc;
            return GetAll(strSQL, CommandType.Text);
        }


        protected override int InsertOnSubmit(tbl_Product item, int t)
        {
            int id = -1;
            String strProName = "ProProduct";
            connec.ExecuteNonQuery(strProName, CommandType.StoredProcedure, new SqlParameter("@flag", t),
                new SqlParameter("@" + tbl_Product.c_Description, item.Description), new SqlParameter("@" + tbl_Product.c_idProduct, item.IdProduct),
                new SqlParameter("@" + tbl_Product.c_imageProduct, item.ImageProduct), new SqlParameter("@" + tbl_Product.c_isActive, item.IsActive),
                new SqlParameter("@" + tbl_Product.c_orderId, item.OrderId),new SqlParameter("@"+tbl_Product.c_Path,item.Path));
            return id;
        }
    }
}
